package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Mr.zhao
 * @version 1.0
 * @date 2021/12/31 11:50
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDao customerDao;

    @Override
    public Map<String, Object> getCustomerList(Integer page, Integer rows, String customerName) {

        List<Customer> list = customerDao.getCustomerList((page -1 ) * rows,rows,customerName);

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows",list);
        result.put("total",list.size());

        return result;
    }

    @Override
    public void saveOrUpdateCustomer(Customer customer) {
        Integer customerId = customer.getCustomerId();
        if (customerId == null ) {
            //新增
            customerDao.save(customer);

        } else {
            //修改
            customerDao.update(customer);
        }
    }

    @Override
    public void delete(String ids) {
        String[] split = ids.split(",");

        customerDao.delete(split);
    }
}
